package kr.re.etri.snsenabler.client;

import kr.re.etri.snsenabler.client.auth.AbstractAuthorizationDialog;
import kr.re.etri.snsenabler.client.bundles.SocialServiceProviderBundle;
import kr.re.etri.snsenabler.shared.Data;
import kr.re.etri.snsenabler.shared.SocialServiceProperties;
import kr.re.etri.snsenabler.shared.SocialServiceProvider;

import com.google.gwt.core.client.GWT;
import com.google.gwt.resources.client.ImageResource;
import com.sencha.gxt.cell.core.client.ButtonCell.ButtonScale;
import com.sencha.gxt.widget.core.client.button.SplitButton;
import com.sencha.gxt.widget.core.client.event.SelectionEvent;
import com.sencha.gxt.widget.core.client.menu.Menu;
import com.sencha.gxt.widget.core.client.menu.MenuItem;

class ServiceProviderButton extends SplitButton {

	private final SocialServiceProviderBundle bundle = GWT.create(SocialServiceProviderBundle.class);

	private final Data user;
	private String userId;
	private final SocialServiceProvider provider;
	private boolean isAuthorized;
	private boolean isUsing;
	private MenuItem useMenuItem;
	private MenuItem notUseMenuItem;
	private MenuItem resetMenuItem;

	protected ServiceProviderButton(Data user, String userId, SocialServiceProvider provider, boolean isAuthorized) {
		super(provider.getTitle());

		this.user = user;
		this.userId = userId;
		this.provider = provider;
		this.isAuthorized = isAuthorized;
		this.isUsing = isAuthorized;

		setIcon(createProviderIcon());
		setScale(ButtonScale.LARGE);
		setMenu(createMenu());
	}

	private ImageResource createProviderIcon() {
		switch (provider) {
			case DaumBlog:
				return isUsing ? bundle.daum_blog() : bundle.daum_blog_gray();
			case DaumCafe:
				return isUsing ? bundle.daum_cafe() : bundle.daum_cafe_gray();
			case Naver:
				return isUsing ? bundle.naver() : bundle.naver_gray();
			case Twitter:
				return isUsing ? bundle.twitter() : bundle.twitter_gray();
			case Facebook:
				return isUsing ? bundle.facebook() : bundle.facebook_gray();
			default:
				break;
		}
		return null;
	}

	private Menu createMenu() {
		Menu menu = new Menu();
		if (isUsing) {
			menu.add(getNotUseMenuItem());
			menu.add(getResetMenuItem());
		} else {
			menu.add(getUseMenuItem());
		}
		return menu;
	}

	private MenuItem getUseMenuItem() {
		if (useMenuItem == null) {
			useMenuItem = new MenuItem("Use", new SelectionEvent.SelectionHandler<MenuItem>() {
				public void onSelection(SelectionEvent<MenuItem> event) {
					if (!isAuthorized) {
						AbstractAuthorizationDialog dialog = AbstractAuthorizationDialog.createDialog(user, provider);
						dialog.getDialog().show();
						Data data = dialog.getSocialServiceData();
						if (data != null) {
							userId = data.getString(SocialServiceProperties.USER_ID);
							ServiceProviderButton.this.isAuthorized = isAuthorized;
							ServiceProviderButton.this.isUsing = isAuthorized;

							setIcon(createProviderIcon());
							setMenu(createMenu());
						}
					} else {
						setIcon(createProviderIcon());
						ServiceProviderButton.this.isUsing = true;
						setMenu(createMenu());
					}
				}
			});
		}
		return useMenuItem;
	}

	private MenuItem getNotUseMenuItem() {
		if (notUseMenuItem == null) {
			notUseMenuItem = new MenuItem("Not Use", new SelectionEvent.SelectionHandler<MenuItem>() {
				public void onSelection(SelectionEvent<MenuItem> event) {
					setIcon(createProviderIcon());
					ServiceProviderButton.this.isUsing = false;
					setMenu(createMenu());
				}
			});
		}
		return notUseMenuItem;
	}

	private MenuItem getResetMenuItem() {
		if (resetMenuItem == null) {
			resetMenuItem = new MenuItem("Reset", new SelectionEvent.SelectionHandler<MenuItem>() {
				public void onSelection(SelectionEvent<MenuItem> event) {
				}
			});
		}
		return resetMenuItem;
	}
}
